// pages/student/mine/setUserInfo/setUserInfo.js
import {
    ajaxPost,
    showToast
}
from '../../utils/util'
let app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        infoWelfareSource: '',
        industrySource: '',
        companySource: '',
        welfareSource: '',
        scaleSource: '',
        natureSource: '',

    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getCommpanyInfo()
    },

    /**
     * 企业信息获取
     */
    getCommpanyInfo() {
        let data = {
            token: wx.getStorageSync("token"),
        }
        ajaxPost("/company/myCompanyInfo", data, (res) => {
            if (res.code == 0) {
                let {
                    company_info,
                    welfare_list,
                    scale_list,
                    nature_list,
                } = res.data
                app.globalData.companState = res.data.company_info.is_authentication
                let welfare = '',
                    industry = '';
                if (company_info.welfare.length > 0) {
                    welfare = company_info.welfare.join('、')
                }
                if (company_info.industry.length > 0) {
                    industry = company_info.industry.join('、')
                }
                this.setData({
                    infoWelfareSource: welfare,
                    industrySource: industry,
                    companySource: company_info,
                    welfareSource: welfare_list,
                    scaleSource: scale_list,
                    natureSource: nature_list,
                })
            } else {
                showToast('text', res.msg);
            }
        });
    },
    /**
     * 改变头像
     */
    changeLogo() {
        let _this = this
        wx.chooseImage({
            count: 1,
            sizeType: ['compressed'],
            sourceType: ['album', 'camera'],
            success(res) {
                let file = res.tempFilePaths[0]
                wx.showLoading({
                    title: '图片上传中',
                })
                wx.uploadFile({
                    url: app.globalData.ajaxUrl + '/upload/index',
                    filePath: file,
                    name: "file",
                    formData: {
                        token: wx.getStorageSync('token')
                    },
                    success: function (e) {
                        let fileData = JSON.parse(e.data)
                        console.log('七牛', fileData);
                        let newFile = fileData.data
                        let data = {
                            logo: newFile,
                        }
                        _this.pustData(data)
                    },
                    fail: function (e) {
                        showToast('error', '上传失败，请重试')
                    },
                    complete: function () {}
                });
            },

        })
    },
    /**
     *  修改个人信息
     * @param {*} data  修改参数
     */
    pustData(data) {
        data.token = wx.getStorageSync('token');
        let _this = this
        ajaxPost("/company/updateCompanyInfo", data, (res) => {
            if (res.code == 0) {
                this.getCommpanyInfo()
                wx.hideLoading();
                showToast('success', '修改成功');
            } else {
                showToast('text', res.msg);
            }
        });

    },

})